package com.jd.platform.async.demo;

import java.util.concurrent.ExecutionException;

import com.jd.platform.async.executor.Async;
import com.jd.platform.async.wrapper.WorkerWrapper;

public class Test01 {

	/**
	 * 使用WorkerWrapper对Worker进行包装以及编排，WorkerWrapper是AysncTool组件的最小可执行任务单元
	 * @param args
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		// 编排顺序C <- B <- A
		// 引入Worker工作单元
		WorkerA workerA = new WorkerA();
		WorkerB workerB = new WorkerB(); 
		WorkerC workerC = new WorkerC();
		
		try {		
    		WorkerWrapper<Integer, Integer> wrapperA = new WorkerWrapper.Builder<Integer, Integer>()
    		        .id("workerA")
                    .worker(workerA)
                    .callback(workerA)
                    .param(1)
    //              .next(wrapperB) // A的next是B
                    .build();
    		
    		WorkerWrapper<Integer, Integer> wrapperB = new WorkerWrapper.Builder<Integer, Integer>()
    		        .id("workerB")
                    .worker(workerB)
                    .callback(workerB)
                    .param(2)
    //              .next(wrapperC) // B的next是C
                    .depend(wrapperA)
                    .build();
    		
    		WorkerWrapper<Integer, Integer> wrapperC = new WorkerWrapper.Builder<Integer, Integer>()
    		        .id("workerC")
        		    .worker(workerC)
        		    .callback(workerC)
        		    .param(3)
        		    .depend(wrapperB)
        		    .build();	
    		
    		Async.beginWork(1500, wrapperA);
		} finally {
		    Async.shutDown();		    
		}
	}
}
